import React, { useEffect } from 'react'
import { message } from 'antd'
import { Navigate,useNavigate} from 'react-router-dom'
import api from '@/api'

export default function Auth({ children }) {
    const nav=useNavigate()
    //从本地获取token信息
    const isToken = localStorage.getItem('token')
    const getAdminByToken = async () => {
        const result = await api.login.getAdminInfoByTokenApi()
        if(result.code==401){
           message.warning('token已失效，请重新登录')
           nav("/login")
        }
    }
    useEffect(() => {
        getAdminByToken()
    }, [])
    if (isToken) {
        return (
            <div>{children}</div>
        )
    } else {
        message.warning('用户还没登录，请先登录')
        return <Navigate to="/login"></Navigate>
    }

}
